package com.craftcomputing.client.SearchResult;

import com.craftcomputing.client.Listeners.SearchResultListeners.*;
import com.google.gwt.user.client.ui.*;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.*;

public class SearchResultsSummary extends Composite {

	/**
	* @wbp.parser.constructor
	*/
	public SearchResultsSummary() {
		makeWidget();
	}
	public SearchResultsSummary(SearchResult searchResult, String query, int numResults) {
		this.query = query;
		this.numResults = numResults;
		this.searchResult = searchResult;
		makeWidget();
	}
	public void makeWidget(){
		final HorizontalPanel searchLabelsPanel = new HorizontalPanel();
		initWidget(searchLabelsPanel);
		searchLabelsPanel.setSize("250px", "100%");
		searchLabelsPanel.setSpacing(2);
		searchLabelsPanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
		
        lastHyperlink = new Hyperlink("<< last", "some history token");
        searchLabelsPanel.add(lastHyperlink);
        searchLabelsPanel.setCellVerticalAlignment(lastHyperlink, HasVerticalAlignment.ALIGN_TOP);
        lastHyperlink.setStyleName("gwt-NextLastLink");
        searchLabelsPanel.setCellHorizontalAlignment(lastHyperlink, HasHorizontalAlignment.ALIGN_CENTER);
        searchLabelsPanel.setCellWidth(lastHyperlink, "50px");
        lastHyperlink.addClickListener(new LastClickListener(searchResult));

        result = new Label("(showing 1 to 10 of 21)");
        searchLabelsPanel.add(result);
        searchLabelsPanel.setCellVerticalAlignment(result, HasVerticalAlignment.ALIGN_TOP);
        searchLabelsPanel.setCellHorizontalAlignment(result, HasHorizontalAlignment.ALIGN_CENTER);
		result.setStyleName("gwt-SearchResultsSummary");		        	
        if(searchResult != null){
			String countStr = "(showing ";
			countStr = countStr + Integer.toString(1+searchResult.getOffset()) + " to "; 
			countStr = countStr + Integer.toString(numResults+searchResult.getOffset()); 
			countStr = countStr + " of " +  searchResult.getCount() + ")";
//	    	String searchText = " Search Results";
//	    	if(query != "")
//	    		searchText = searchText + " for " + query;
//	    	result.setText(countStr + searchText);
	    	result.setText(countStr);
        }


        nextHyperlink = new Hyperlink("next >>", "some history token");
		searchLabelsPanel.add(nextHyperlink);
		searchLabelsPanel.setCellVerticalAlignment(nextHyperlink, HasVerticalAlignment.ALIGN_TOP);
		nextHyperlink.setStyleName("gwt-NextLastLink");
		searchLabelsPanel.setCellHorizontalAlignment(nextHyperlink, HasHorizontalAlignment.ALIGN_CENTER);
		searchLabelsPanel.setCellWidth(nextHyperlink, "50px");
		nextHyperlink.addClickListener(new NextClickListener(searchResult));

	}
	public void update() {
		int count = Integer.parseInt(searchResult.getCount()); 
		int offset = searchResult.getOffset();
		if (count > numResults){
			String countStr = "(showing ";
			countStr = countStr + Integer.toString(1+offset) + " to "; 
			countStr = countStr + Integer.toString(offset + (count-offset > numResults ? numResults : count-offset)); 
			countStr = countStr + " of " +  searchResult.getCount() + ")";
	    	result.setText(countStr);
			nextHyperlink.setVisible((count-offset <= numResults) ?  false : true);
			lastHyperlink.setVisible((offset == 0) ?  false : true);
		}
		else {
			String countStr = "(showing ";
			countStr = countStr + Integer.toString(1+offset) + " to "; 
			countStr = countStr + Integer.toString(count+offset) + ")"; 
	    	result.setText(countStr);			
			nextHyperlink.setVisible(false);
			lastHyperlink.setVisible(false);
		}
	}
	public String getQuery() {return query;}
	private SearchResult searchResult;
	private int numResults;
	private String query;
	private Label result;
	private Hyperlink nextHyperlink;
	private Hyperlink lastHyperlink;
}
